import 'dart:convert';

import 'package:after_sale_plugin/package_event.dart';
import 'package:after_sale_plugin/pages/edit_order/cell/check_vehicle_error_result_item.dart';
import 'package:after_sale_plugin/pages/order_detail/cell/media_info_view.dart';
import 'package:after_sale_plugin/pages/order_detail/cell/problem_content_view.dart';
import 'package:after_sale_plugin/pages/order_detail/cell/info_detail_view.dart';
import 'package:after_sale_plugin/pages/order_detail/controller.dart';
import 'package:after_sale_plugin/pages/order_detail/cell/order_progress_view.dart';
import 'package:after_sale_plugin/routes.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:wechat_assets_picker_plus/wechat_assets_picker_plus.dart';

class OrderDetailView extends BaseGetView<OrderDetailController> {
  OrderDetailView({super.key});

  @override
  String get title => AppLanguageUtil.to.orderDetailNavTitle;

  @override
  Color? get barBackgroundColor => Colors.black;

  @override
  Widget? get backView => CommonWidgets.appBarLeading();

  @override
  Widget buildBody(BuildContext context) {
    return Obx(
      () => Scaffold(
        backgroundColor: Colors.black,
        body: ListView(
          children: [
            // 订单进度视图
            OrderProgressView(state: controller.state.orderProgressState),
            // 问题解决视图
            _buildProblemResolutionView(),
            // 问题描述视图
            _buildProblemDescriptionView(),
            // 反馈资产视图
            _buildFeedbackAssetsView(),
            // 订单信息视图
            _buildOrderInfoView(),
            // 维修详情视图
            _buildRepairDetailView(),
            // 维修资产视图
            _buildRepairAssetsView(),
            // 订单历史视图
            _buildOrderHistoryView(),
          ],
        ),
      ),
    );
  }

  // 构建问题解决视图
  Widget _buildProblemResolutionView() {
    return Visibility(
      visible: controller.state.problemResolutionContent.isNotEmpty,
      child: ProblemContenView(
        title: AppLanguageUtil.to.titleProblemResolution,
        orderProgressState: controller.state.orderProgressState,
        problemDescContent: controller.state.problemResolutionContent,
        problemResolvedState: controller.state.problemResolvedState,
        feedbackOnTap: (p0) {
          if (p0 == ProblemResolvedState.resolved) {
            controller.orderResolved();
          } else {
            Get.toNamed(Routes.problemResolution,
                    arguments: controller.state.id)
                ?.then((id) {
              controller.getOrderDetail(id);
            });
          }
        },
      ),
    );
  }

  // 构建问题描述视图
  Widget _buildProblemDescriptionView() {
    return Visibility(
      visible: controller.state.problemDescContent.isNotEmpty,
      child: ProblemContenView(
        title: AppLanguageUtil.to.titleProblemDesc,
        orderProgressState: controller.state.orderProgressState,
        problemDescContent: controller.state.problemDescContent,
        problemResolvedState: ProblemResolvedState.none.obs,
      ),
    );
  }

  // 构建反馈资产视图
  Widget _buildFeedbackAssetsView() {
    return Visibility(
      visible: controller.state.showFeedbackAssets.value,
      child: MediaInformationView(
        title: AppLanguageUtil.to.titleFeedback,
        assets: controller.state.supplementAssets,
        assetsContent: controller.state.supplementContent.value,
        isEdit: controller.state.needToSupplementAssets,
        content: controller.state.feedbackContent,
        allowedSubmit: controller.state.allowedSubmit,
        callbackTextFeildValue: (p0) {
          controller.state.supplementContent.value = p0;
        },
        addAssetOnTap: () {
          PackageEvent.selectAsssets(
            (p0) {
              AssetModel assetModel = AssetModel.fromMap(jsonDecode(p0));
              bool isFull = controller.state.supplementAssets.length >= 9;
              if (isFull) {
                controller.state.supplementAssets.value = controller
                    .state.supplementAssets
                    .where((element) => !element.isAdd)
                    .toList();
              }
              controller.state.supplementAssets.insert(
                controller.state.supplementAssets.length - (isFull ? 0 : 1),
                assetModel,
              );
              PackageEvent.uploadAsssets(
                (fileName) {
                  AssetModel tempModel = assetModel
                    ..src = AssetModel.getUrl(OSS_FILE_URL, fileName);
                  for (var i = 0;
                      i < controller.state.supplementAssets.length;
                      i++) {
                    if (controller.state.supplementAssets[i].filePath ==
                        tempModel.filePath) {
                      controller.state.supplementAssets[i] = tempModel;
                    }
                  }
                },
                param: assetModel.filePath,
              );
            },
            param: 9 - controller.state.supplementAssets.length + 1,
          );
        },
        submitOnTap: controller.submitAgain,
      ),
    );
  }

  // 构建订单信息视图
  Widget _buildOrderInfoView() {
    return InformationDetailView(
      title: AppLanguageUtil.to.titleOrderInfo,
      itemList: controller.state.orderInfos,
    );
  }

  // 构建维修详情视图
  Widget _buildRepairDetailView() {
    return InformationDetailView(
      title: AppLanguageUtil.to.titleOrderRepairDetail,
      itemList: controller.state.repairDetails,
      extendedWidget: controller.state.reportOrderInfo.value.diagnosis.value
                  .descriptions.value ==
              null
          ? null
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: CONTENT_MARGIN / 2),
                LxText(
                  AppLanguageUtil.to.titleCheckError,
                  color: Colors.white,
                  fontWeightEnum: LxFontWeightEnum.bodyMedium,
                ),
                CheckVehicleErrorResultItem(
                  reportOrderInfo: controller.state.reportOrderInfo,
                ),
              ],
            ),
    );
  }

  // 构建维修资产视图
  Widget _buildRepairAssetsView() {
    return Visibility(
      visible: controller.state.repairAssets.isNotEmpty,
      child: MediaInformationView(
        title: AppLanguageUtil.to.titlePhoptoAndVideo,
        assets: controller.state.repairAssets,
        isEdit: false.obs,
        allowedSubmit: controller.state.allowedSubmit,
      ),
    );
  }

  // 构建订单历史视图
  Widget _buildOrderHistoryView() {
    return InformationDetailView(
      title: AppLanguageUtil.to.titleOrderHistory,
      itemList: controller.state.orderHistorys,
    );
  }
}
